package com.itheima.student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList<Student> list=new ArrayList<>();
        ed:
        while (true){
            System.out.println("-----------欢迎来到学生管理---------");
            System.out.println("1.添加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.查看学生信息");
            System.out.println("5.退出系统");
            System.out.println("请输入您的选择：");
           String choice=sc.next();
            switch (choice){
                case "1":
                    addStudent(list);
                    break;
                case "2":
                   removeStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    break ed;

            }
        }

    }

    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改学生信息的学号：");
        String id=sc.next();
        int index=getIndex(list,id);
        if(index==-1){
            System.out.println("学号不存在，请重新输入");
        }else
            System.out.println("请输入姓名");
            String name=sc.next();
            System.out.println("请输入年龄");
            int age=sc.nextInt();
            System.out.println("请输入生日");
            String brithday=sc.next();
            Student stu=new Student(id,name,age,brithday);
            list.set(index,stu);
            System.out.println("修改成功！");
    }

    public static void removeStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除学生信息的学号：");
        String id=sc.next();
        int index=getIndex(list,id);

            if (index == -1){
                System.out.println("学号不存在，请重新输入");
            }else {
                list.remove(index);
                System.out.println("删除成功");

            }
        }


    public static int getIndex(ArrayList<Student> list, String sid){

        int index=-1;
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            String id=stu.getSid();
            if(id.equals(sid)){
                index=i;
            }
        }
        return index;
    }

    public static void queryStudent(ArrayList<Student> list) {
        if (list.size()==0){
            System.out.println("暂无学生信息，请添加");
            return;
        }
        System.out.println("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"生日");
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            System.out.println(stu.getSid()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getBrithday());
        }
    }

    public static void addStudent(ArrayList<Student> list) {
    Scanner sc=new Scanner(System.in);
    String sid;
    while (true){
        System.out.println("请输入学号");
        sid=sc.next();
        int index=getIndex(list,sid);
        if (index==-1){
            break;
        }
    }

        System.out.println("请输入姓名");
        String name=sc.next();
        System.out.println("请输入年龄");
        int age=sc.nextInt();
        System.out.println("请输入生日");
        String brithday=sc.next();
        Student stu=new Student(sid,name,age,brithday);
        list.add(stu);
        System.out.println("学生信息已经添加成功！");
    }
}
